package com.zdsoft.crm.dao.clue;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zdsoft.crm.entity.Clue;
import com.zdsoft.crm.vo.clue.ClueListVo;

public interface ICluePoolMapper {

	
	@Select("SELECT c.*,user_create.username createName  FROM("
			+ " SELECT A.*,ROWNUM RN FROM  (SELECT * FROM CLUE ) A WHERE ROWNUM<=#{endIndex})  c "
			+ "  left join SYS_USER user_create on c.CREATE_USER_ID = user_create.id "
			+ "   WHERE RN > #{startIndex}"
			+ "    and CHARGE_USER_ID is null")
	List<ClueListVo> pageList(@Param("startIndex") Integer startIndex, @Param("endIndex") Integer endIndex);
	
	@Select("<script>"
			+ " SELECT c.*,user_create.username createName  FROM   (       "
			+ "  	select A.* ,ROWNUM rn"
			+ "		from ("
			+ "				select *   from CLUE"
			+ " 				<choose>  "
			+ " 					<when test='CriteriaType==\"COMPANY_NAME\"'>  "
			+ "    						  where COMPANY_NAME like '%'||#{content}||'%'  "
            + "						 </when>  "
            + "						<when test='CriteriaType==\"CONTACT_NAME\"'>  "
           	+ "    						 where CONTACT_NAME like '%'||#{content}||'%'  "
            + " 					</when>  "
            + "					</choose>  "
			+ "				) A"
			+ "		<if test='endIndex != null and endIndex!=\"\"' >"
			+ " 			<![CDATA[where ROWNUM <=#{endIndex}]]>"
			+ "		</if>  ) c"
			+ " 	left join SYS_USER user_charge on c.CHARGE_USER_ID = user_charge.id "
			+ " 	left join SYS_USER user_create on c.CREATE_USER_ID = user_create.id "
			+ " <where>"
			+ " <if test='startIndex != null and startIndex!=\"\"' >"
			+ " 		AND rn > #{startIndex}"
			+ " </if>"
			+ " <if test='1==1' >"
			+ " 		AND CHARGE_USER_ID is null"
			+ " </if>"
            + "<if test='CriteriaType==\"createName\"'>  "
           	+ "     AND user_create.username  like '%'||#{content}||'%' "
            + " </if>  "
            + " </where> "
			+"</script>")
	List<ClueListVo> pageListWhere(@Param("startIndex") Integer startIndex, @Param("endIndex") Integer endIndex,@Param("CriteriaType") String CriteriaType,@Param("content") String content);
	
	
	@Select("<script>"
			+ " SELECT count(c.id)  FROM   (       "
			+ "  	select A.* ,ROWNUM rn"
			+ "		from ("
			+ "				select *   from CLUE"
			+ " 				<choose>  "
			+ " 					<when test='CriteriaType==\"COMPANY_NAME\"'>  "
			+ "    						  where COMPANY_NAME like '%'||#{content}||'%'  "
            + "						 </when>  "
            + "						<when test='CriteriaType==\"CONTACT_NAME\"'>  "
           	+ "    						 where CONTACT_NAME like '%'||#{content}||'%'  "
            + " 					</when>  "
            + "					</choose>  "
			+ "				) A"
			+ "		  ) c"
			+ " 	left join SYS_USER user_charge on c.CHARGE_USER_ID = user_charge.id "
			+ " 	left join SYS_USER user_create on c.CREATE_USER_ID = user_create.id "
			+ " <where>"
			+ " <if test='1==1' >"
			+ " 		AND CHARGE_USER_ID is null"
			+ " </if>"
            + "<if test='CriteriaType==\"createName\"'>  "
           	+ "     AND user_create.username  like '%'||#{content}||'%' "
            + " </if>  "
            + " </where> "
			+"</script>")
	Long totalCount(@Param("CriteriaType") String CriteriaType,@Param("content") String content);
	
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Delete("delete from CLUE where id = #{id}")
	public int delete(String id);
	
	@Update("update CLUE set CHARGE_USER_ID=#{charge_user_id} where id=#{id}")
	public int getMyClue(Clue clue);
	
	@Update("update CLUE set CHARGE_USER_ID=#{userId} where id=#{clueId}")
	public int distributeClue(@Param("clueId") String clueId,@Param("userId") String userId);
	
	
}
